/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.lvyh.lightframe.sharding.sample.masterslave.controller;

import com.lvyh.lightframe.sharding.sample.masterslave.bean.UserBean;
import com.lvyh.lightframe.sharding.sample.masterslave.bean.UserParamInfo;
import com.lvyh.lightframe.sharding.sample.masterslave.service.UserInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author lvyh 2021/03/21.
 */
@RestController
@Slf4j
public class UserController {

    @Autowired
    private UserInfoService userInfoService;

    //http://localhost:8080/save
    @GetMapping("save")
    public void save() throws Exception {
        UserBean user = new UserBean();
        user.setUsername("梅西");
        user.setPassword("111111");
        user.setSex(1);
        user.setAge(30);
        long id = userInfoService.save(user);
        log.info("add user:{}", id);
    }

    //http://localhost:8080/update
    @GetMapping("update")
    public void update() throws Exception {
        UserBean user = new UserBean();
        user.setId(40L);
        user.setUsername("梅西123");
        user.setPassword("123");
        user.setSex(0);
        user.setAge(35);
        boolean result = userInfoService.update(user);
        log.info("update user:{}", result);
    }

    //http://localhost:8080/delete
    @GetMapping("delete")
    public void delete() throws Exception {
        UserBean user = new UserBean();
        user.setId(40L);
        boolean result = userInfoService.deleteUser(user);
        log.info("delete user:{}", result);
    }

    //http://localhost:8080/selectById
    @GetMapping("selectById")
    public void selectById() throws Exception {
        UserBean user = new UserBean();
        user.setId(21L);
        UserBean userBean = userInfoService.selectById(user);
        log.info("selectById:{}", userBean != null ? userBean.getUsername() : "");
    }

    //http://localhost:8080/selectList
    @GetMapping("selectList")
    public void selectList() throws Exception {
        UserParamInfo param = new UserParamInfo();
        param.setAge(35);
        List<UserBean> userList = userInfoService.selectList(param);
        log.info("selectList:{}", userList);
    }

    //http://localhost:8080/selectListByPage
    @GetMapping("selectListByPage")
    public void selectListByPage() throws Exception {
        UserParamInfo param = new UserParamInfo();
        param.setPageNum(3);
        param.setPageSize(5);
        List<UserBean> userList = userInfoService.selectListByPage(param);
        log.info("selectListByPage:{}", userList);
    }


}
